﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;
using Portal.Exam.Model;
using Portal.Exam.Bussiness;
using Portal.Web.Model;
using Portal.Web.Utility;
using Portal.Core;

namespace Portal.Exam.Render
{
    public class QuestionTreeViewRenderBll
    {
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut Draw(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                string RootQuestionId = (string)WebEnvironments.Request(SiteId, "RId");
                if (string.IsNullOrEmpty(RootQuestionId)) RootQuestionId = "";

              

                string Html =
                    "<script language=javascript>\r\n"+
                    "     function BackFromAddQuestion()\r\n"+
                    "     {\r\n"+
                    "         document.getElementById('divFormAddQuestion').innerHTML='';\r\n" +
                    "         document.getElementById('divFormAddQuestion').style.display='none';\r\n" +
                    "         document.getElementById('divMainForm').style.display='block';\r\n" +
                    "     }\r\n"+
                    
                    "     function BackFromUpdateQuestion()\r\n" +
                    "     {\r\n" +
                    "         document.getElementById('divFormUpdateQuestion').innerHTML='';\r\n" +
                    "         document.getElementById('divFormUpdateQuestion').style.display='none';\r\n" +
                    "         document.getElementById('divMainForm').style.display='block';\r\n" +
                    "     }\r\n" +

                    "     function AddTreeNode(id,title)\r\n"+
                    "     {\r\n"+
                    "           var SelectedNode = $(\"#tree\").dynatree(\"getActiveNode\");\r\n" +
                    "           var childNode=null;\r\n"+
                    "           if(SelectedNode!=null)\r\n"+
                    "           {\r\n"+
                    "               childNode = SelectedNode.addChild({\r\n"+
                    "                       isFolder: true\r\n"+
                    "               });\r\n"+
                    "               childNode.data.href=id;\r\n"+
                    "               childNode.setTitle(title);\r\n"+
                    "           }\r\n"+
                    "     }\r\n"+

                    "     function SaveTreeNode(id,title)\r\n" +
                    "     {\r\n"+
                    "          var SelectedNode = $(\"#tree\").dynatree(\"getActiveNode\");\r\n" +
                    "          SelectedNode.setTitle(title);\r\n" +
                    "     }\r\n"+


                    "     function ClientAddQuestion(RId)\r\n" +
                    "     {\r\n" +
                    "         SiteId='"+SiteId+"';\r\n"+
                    "         ParentQuestionId=document.getElementById('txtSelectedQuestionId').value;\r\n" +
                    "         RootQuestionId='"+RootQuestionId+"';\r\n"+
                    "         AjaxOut = Portal.Exam.Render.QuestionTreeViewRenderBll.ServerSideGetAddQuestion(SiteId,RootQuestionId, ParentQuestionId).value;\r\n" +
                    "         if(AjaxOut.Error)\r\n" +
                    "         {\r\n" +
                    "           alert(AjaxOut.InfoMessage);\r\n" +
                    "           return;\r\n" +
                    "         }\r\n" +
                    "         document.getElementById('divFormAddQuestion').innerHTML=AjaxOut.HtmlContent;\r\n"+
                    "         document.getElementById('divFormAddQuestion').style.display='block';\r\n"+
                    "         document.getElementById('divMainForm').style.display='none';\r\n" +
                    "     }\r\n" +

                    "     function ClientUpdateQuestion(RId)\r\n" +
                    "     {\r\n" +
                    "         QuestionId=document.getElementById('txtSelectedQuestionId').value;\r\n" +
                    "         AjaxOut = Portal.Exam.Render.QuestionTreeViewRenderBll.ServerSideGetUpdateQuestion(SiteId,QuestionId).value;\r\n" +
                    "         if(AjaxOut.Error)\r\n" +
                    "         {\r\n" +
                    "           alert(AjaxOut.InfoMessage);\r\n" +
                    "           return;\r\n" +
                    "         }\r\n" +
                    "         document.getElementById('divFormUpdateQuestion').innerHTML=AjaxOut.HtmlContent;\r\n"+
                    "         document.getElementById('divFormUpdateQuestion').style.display='block';\r\n" +
                    "         document.getElementById('divMainForm').style.display='none';\r\n" +
                    "     }\r\n" +



                    "     function ClientDeleteQuestion(RId)\r\n" +
                    "     {\r\n" +
                    "         QuestionId=document.getElementById('txtSelectedQuestionId').value;\r\n" +
                    "         if(QuestionId=='')\r\n"+
                    "         {\r\n"+
                    "               alert('Chưa xác định thư mục cần xóa');\r\n"+
                    "               return;\r\n"+
                    "         }\r\n"+
                    "         if(QuestionId==RId)\r\n" +
                    "         {\r\n" +
                    "               alert('Không xóa được thư mục gốc');\r\n" +
                    "               return;\r\n" +
                    "         }\r\n" +
                    "         if(confirm('Bạn đã chắc chắn xóa chưa?')==false)return;\r\n"+
                    "         AjaxOut = Portal.Exam.Render.QuestionTreeViewRenderBll.ServerSideDeleteQuestion(SiteId,QuestionId).value;\r\n" +
                    "         if(AjaxOut.Error)\r\n" +
                    "         {\r\n" +
                    "           alert(AjaxOut.InfoMessage);\r\n" +
                    "           return;\r\n" +
                    "         }\r\n" +
                    "         SelectedNode = $(\"#tree\").dynatree(\"getActiveNode\");\r\n" +
                    "         SelectedNode.remove();\r\n"+
                    "         tree.activateKey(RId);\r\n"+
                    "         ClientLoadQuestion(RId);\r\n"+
                    "     }\r\n" +

                    "   function RemoveQuestion(QuestionId,QuestionId)\r\n" +
                    "   {\r\n" +
                    "       if(confirm('Bạn đã chắc chắn loại bỏ câu hỏi khỏi thư mục này chưa?')==false)return;\r\n" +
                    "       SiteId='" + SiteId + "';\r\n" +
                    "       AjaxOut = Portal.Exam.Render.QuestionTreeViewRenderBll.ServerSideRemoveQuestionFromQuestion(SiteId,QuestionId,QuestionId).value;\r\n" +
                    "       if(AjaxOut.Error)\r\n" +
                    "       {\r\n" +
                    "           alert(AjaxOut.InfoMessage);\r\n" +
                    "           return;\r\n" +
                    "       }\r\n" +
                    "       document.getElementById('divQuestionList').innerHTML=AjaxOut.HtmlContent;\r\n" +
                    "   }\r\n" +
                    
                    
                    "   function SearchExamQuestion()\r\n"+
                    "   {\r\n"+
                    "         SiteId='"+SiteId+"';\r\n"+
                    "         QuestionId=document.getElementById('txtSelectedQuestionId').value;\r\n" +
                    "         if(QuestionId=='')\r\n" +
                    "         {\r\n" +
                    "               alert('Chưa xác định thư mục cần đưa câu hỏi vào');\r\n" +
                    "               return;\r\n" +
                    "         }\r\n" +
                    "       AjaxOut = Portal.Exam.Render.QuestionTreeViewRenderBll.ServerSideGetSearchForm(SiteId,'"+RootQuestionId+"',QuestionId).value;\r\n" +
                    "       if(AjaxOut.Error)\r\n"+
                    "       {\r\n"+
                    "           alert(AjaxOut.InfoMessage);\r\n"+
                    "           return;\r\n"+
                    "       }\r\n"+
                    "       document.getElementById('divSearchExamQuestion').innerHTML=AjaxOut.HtmlContent;\r\n"+
                    "       document.getElementById('divSearchExamQuestion').style.display='block';\r\n"+
                    "       document.getElementById('divMainForm').style.display='none';\r\n" +
                    "   }\r\n"+


                    "   function BackFromSearchExamQuestion()\r\n" +
                    "   {\r\n" +
                    "       document.getElementById('divSearchExamQuestion').innerHTML='';\r\n" +
                    "       document.getElementById('divSearchExamQuestion').style.display='none';\r\n" +
                    "       document.getElementById('divMainForm').style.display='block';\r\n" +
                    "   }\r\n" +
                    "</script>\r\n"+
                    QuestionRenderBll.GetPlugInAddQuestionJavascript(SiteId,true)+
                    QuestionRenderBll.GetPlugInUpdateQuestionJavascript(SiteId, true) +
                    SearchExamQuestion.GetPluginJavascript(SiteId)+

                    "<div id=divFormAddQuestion></div>\r\n" +
                    "<div id=divFormUpdateQuestion></div>\r\n" +
                    "<div id=divSearchExamQuestion></div>\r\n"+
                    "<div class=CssFormData id=divMainForm>\r\n" +
                        DrawFunction(SiteId, RootQuestionId).HtmlContent +
                    "<table>\r\n" +
                    "   <tr>\r\n" +
                    "       <td class=col1><div class=CssDivQuestion>" + DrawQuestion(SiteId, RootQuestionId).HtmlContent + "</div></td>\r\n" +
                    "       <td class=col2>\r\n"+
                    "           <h3>Danh sách câu hỏi trong thư mục<input type=button class=button style=\"float:right;margin-right:10px;\" value=\"Điều kiện tìm kiếm\"></h3>\r\n"+
                    "           <div class=CssQuestionList id=\"divQuestionList\">" +
                                       ServerSideReadingQuestionInQuestion(SiteId, RootQuestionId).HtmlContent + "\r\n" +
                    "           </div>\r\n"+
                    "       </td>\r\n" +
                    "   </tr>\r\n" +
                    "</table>\r\n" +
                    "</div>\r\n";
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut DrawQuestion(string SiteId,string RootQuestionId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                QuestionCls OQuestion=QuestionBll.CreateQuestionModel(SiteId,RootQuestionId);
                string Html =
                              "<script language=javascript>\r\n"+
                              "     function ClientLoadQuestion(QuestionId)\r\n"+
                              "     {\r\n"+
                              "         SiteId='"+SiteId+"';\r\n"+
                              "         document.getElementById('txtSelectedQuestionId').value = QuestionId;\r\n" +
                              "         AjaxOut = Portal.Exam.Render.QuestionTreeViewRenderBll.ServerSideReadingQuestionInQuestion(SiteId,QuestionId).value;\r\n"+
                              "         document.getElementById('divQuestionList').innerHTML=AjaxOut.HtmlContent;\r\n" +
                              "     }\r\n"+
                              "</script>\r\n"+
                              "<input type=hidden id=\"txtSelectedQuestionId\" value=\"" + RootQuestionId + "\">\r\n" +
                              "<h3><a href=\"QuestionInputDataTreeView.aspx\">" + OQuestion.QuestionName + "</a></h3>\r\n" +
                              "<div class=TreeHolder>\r\n"+
                              "<div id=\"tree\">\r\n"+
                              "<ul>\r\n"+
                              " <li class=\"folder expanded\"><a href=\"" + RootQuestionId + "\" target=\"_self\">" + OQuestion.QuestionName + "</a>\r\n" +
                              "     <ul>\r\n";
                QuestionCls[] Questions = QuestionBll.ReadingQuestion(SiteId, RootQuestionId);
                for (int iIndex = 0; iIndex < Questions.Length; iIndex++)
                {
                    Html +=
                            "<li class=\"folder expanded\"><a href=\"" +  Questions[iIndex].QuestionId + "\" target=\"_self\">" + Questions[iIndex].QuestionName + "</a>\r\n" +
                                BuildSubTreeView(SiteId,Questions[iIndex].QuestionId).HtmlContent+
                            "</li>\r\n";
                }

                Html += "</ul>\r\n" +
                        "</li>\r\n"+
                        "</ul>\r\n"+
                        "</div>\r\n" +
                        "</div>\r\n";
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut BuildSubTreeView(string SiteId,string ParentQuestionId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                string Html = "";
                MonthiCls[] Monthi = new MonthiBll().ReadingMonthi(SiteId, ParentQuestionId);
                Html+=
                    "<ul>\r\n";
                for (int iIndex = 0; iIndex < Questions.Length; iIndex++)
                {
                    Html +=
                            "<li class=\"folder expanded\"><a target=\"_self\" href=\"" + Questions[iIndex].QuestionId + "\">" + Questions[iIndex].QuestionName + "</a>\r\n" +
                                BuildSubTreeView(SiteId,  Questions[iIndex].QuestionId).HtmlContent +
                            "</li>\r\n";
                }

                Html += "</ul>\r\n";
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut DrawFunction(string SiteId,string RootQuestionId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                string IconRoot = WebConfig.GetButtonIconRoot(OSiteParam);

                string Html =
                   "<div class=FormFunction id=divListForm>\r\n" +
                   " <div class=DatabaseFunction>\r\n" +
                   "        <ul>\r\n" +
                   "            <li onclick=\"javascript:ClientAddQuestion('" + RootQuestionId + "');\"><img class=image src=\""+IconRoot+"/Add.png\"><div>Tạo thư mục</div></li>\r\n" +
                   "            <li onclick=\"javascript:ClientDeleteQuestion('" + RootQuestionId + "');\"><img class=image src=\""+IconRoot+"/Delete.png\"><div>Xóa thư mục</div></li>\r\n" +
                   "            <li onclick=\"javascript:ClientUpdateQuestion('"+RootQuestionId+"');\"><img class=image src=\""+IconRoot+"/Update.png\"><div>Sửa thư mục</div></li>\r\n" +
                   "            <li onclick=\"javascript:ClientMoveQuestion();\"><img class=image src=\""+IconRoot+"/Move.png\"><div>Chuyển</div></li>\r\n" +
                   //"            <li onclick=\"javascript:ClientAddQuestionData();\"><img class=image src=\"Resources/Images/AddQuestion.png\"><div>Thêm tài liệu</div></li>\r\n" +
                   "            <li onclick=\"javascript:SearchExamQuestion();\"><img class=image src=\""+IconRoot+"/selection.png\"><div>Chọn câu hỏi</div></li>\r\n" +
                   "            <li onclick=\"javascript:window.open('Question.aspx','_self');\"><img class=image src=\"" + IconRoot + "/back.png\"><div>Quay lại</div></li>\r\n" +
                   "        </ul>\r\n" +
                   "  </div>\r\n" +
                   "</div>\r\n";
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideReadingQuestionInQuestion(
            string SiteId, 
            string QuestionId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                string Html = "";
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                string IconRoot = WebConfig.GetButtonIconRoot(OSiteParam);

                QuestionCls OQuestion = null;
                if (!string.IsNullOrEmpty(QuestionId))
                {
                    OQuestion = QuestionBll.CreateQuestionModel(SiteId, QuestionId);
                }
                
                if (OQuestion != null)
                {
                    Html +=
                        "<h3>Đang chọn thư mục: " + OQuestion.QuestionName + "</h3>";
                }
                DataTable dtQuestion = QuestionBll.ReadingQuestionForQuestion(SiteId,  QuestionId);
                if (dtQuestion.Rows.Count == 0)
                {
                    Html += "<div class=CssNoData>Không có câu hỏi nào thuộc bộ sưu tập</div>\r\n";
                }
                else
                {
                    Html += "<div class=tabcontent id=divListQuestionResult>\r\n" +
                           "    <table>\r\n" +
                           "        <tr>\r\n" +
                           "            <th class=thIndex>#</th>\r\n" +
                           "            <th>Câu hỏi</th>\r\n" +
                           "            <th>Kiểu</th>\r\n" +
                           "            <th></th>\r\n" +
                           "            <th></th>\r\n" +
                           //"            <th></th>\r\n" +
                           //"            <th></th>\r\n" +
                           "        </tr>\r\n";
                    for (int iIndex = 0; iIndex < dtQuestion.Rows.Count; iIndex++)
                    {
                        string QuestionId = (string)dtQuestion.Rows[iIndex]["QuestionId"];
                        string Type = (string)dtQuestion.Rows[iIndex]["Type"];
                        string Title = (string)dtQuestion.Rows[iIndex]["Title"];
                        Html+=
                           "        <tr>\r\n" +
                           "            <td class=tdIndex>"+(iIndex+1)+"</td>\r\n" +
                           "            <td><span class=CssQuestionSubject><a href=\"#\">" + Title + "</a></span></td>\r\n" +
                           "            <td>" + Type + "</td>\r\n" +
                           "            <td class=tdButton><a href=\"javascript:RemoveQuestion('" + QuestionId + "','" + QuestionId + "');\"><img alt=\"Loại khỏi thư mục\" title=\"Loại khỏi thư mục\" class=imagebutton src=\"" + IconRoot + "/Remove.png\"></a></td>\r\n" +
                           //"            <td class=tdButton><a href=\"javascript:UpdateQuestion('" + QuestionId + "','" + QuestionId + "');\"><img alt=\"Sửa thuộc tính tài liệu\" title=\"Sửa thuộc tính tài liệu\" class=imagebutton src=\"Resources/Images/Update.png\"></a></td>\r\n" +
                           "            <td class=tdButton><a href=\"javascript:ViewQuestion('" + QuestionId + "','" + QuestionId + "');\"><img alt=\"Xem câu hỏi\" title=\"Xem câu hỏi\" class=imagebutton src=\""+IconRoot+"/preview.png\"></a></td>\r\n" +
                           //"            <td class=tdButton><a href=\"AddDataToQuestion.aspx?QuestionId=" + QuestionId + "&QuestionId=" + QuestionId + "\"><img alt=\"Phân vào thư mục\" title=\"Phân vào thư mục\" class=imagebutton src=\"Resources/Images/Category.png\"></a></td>\r\n" +
                           "        </tr>\r\n";
                    }
                    Html += "</table>\r\n" +
                            "</div>\r\n";
                }
                OAjaxOut.HtmlContent = Html;
                    
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }





        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideGetAddQuestion(
            string SiteId,
            string RootQuestionId,
            string ParentQuestionId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                OAjaxOut =
                    QuestionRenderBll.DrawAddQuestionForAjax(SiteId, RootQuestionId, ParentQuestionId, "javascript:BackFromAddQuestion();");
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }


        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideGetUpdateQuestion(
            string SiteId,
            string QuestionId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                OAjaxOut =
                    QuestionRenderBll.DrawUpdateQuestionForAjax(SiteId, QuestionId, "javascript:BackFromUpdateQuestion();");
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }
        
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideDeleteQuestion(
            string SiteId,
            string QuestionId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                QuestionBll.DeleteQuestion(SiteId, QuestionId);
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideGetSearchForm(
            string SiteId,
            string RootQuestionId,
            string QuestionId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                OAjaxOut.HtmlContent = SearchExamQuestion.Draw(SiteId, QuestionId).HtmlContent;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideRemoveQuestionFromQuestion(
            string SiteId,
            string QuestionId,
            string QuestionId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                QuestionBll.RemoveCollectionFromExam(SiteId, QuestionId, QuestionId);
                OAjaxOut.HtmlContent = ServerSideReadingQuestionInQuestion(SiteId, QuestionId).HtmlContent;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }


    }
}
